package com.bingxue.edu.parent.util;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpLogic;
import com.bingxue.edu.framework.common.constant.CacheConstants;
import com.bingxue.edu.framework.common.util.SpringContextHolder;
import com.bingxue.edu.management.education.model.entity.Parent;
import com.bingxue.edu.management.education.service.ParentService;
import com.bingxue.edu.parent.model.AuthParent;
import com.bingxue.edu.parent.service.ParentAuthService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author panbo
 * @since 2024/8/24
 */
@Slf4j
public class ParentAuthUtil {

    public static final StpLogic STP = new StpLogic("parent");

    static {
        configParentStpLogic();
    }

    /**
     * 获取当前登录用户id
     */
    public static String getCurrentParentId() {
        Object loginId = STP.getLoginIdDefaultNull();
        return loginId == null ? null : loginId.toString();
    }

    /**
     * 获取当前学生id
     */
    public static String getCurrentStudentId() {
//        Object loginId = STP.getLoginIdDefaultNull();
//        return loginId == null ? null : loginId.toString();
        // TODO
        return null;
    }

    /**
     * 登录并缓存用户信息
     * @param authParent 认证用户信息
     */
    public static String login(AuthParent authParent) {
        STP.login(authParent.getUserInfo().getId());
        String tokenValue = STP.getTokenValue();
        authParent.setToken(tokenValue);
        SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, authParent);
        STP.getTokenSession().set(CacheConstants.LOGIN_USER_KEY, authParent);
        return tokenValue;
    }

    /**
     * 获取登录用户信息
     *
     * @return 登录用户信息
     * @throws NotLoginException 未登录异常
     */
    public static AuthParent getAuthParent() throws NotLoginException {
        STP.checkLogin();
        AuthParent authParent = (AuthParent)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_KEY);
        if (null != authParent) {
            return authParent;
        }
        SaSession tokenSession = STP.getTokenSession();
        authParent = (AuthParent)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
        if (null == authParent) {
            log.warn("家长用户信息为空, token: {}, loginId: {}", STP.getTokenValue(), STP.getLoginId());
            ParentService parentService = SpringContextHolder.getBean(ParentService.class);
            ParentAuthService parentAuthService = SpringContextHolder.getBean(ParentAuthService.class);
            Parent parent = parentService.getById(STP.getLoginIdAsString());
            authParent = parentAuthService.buildAuthParent(parent);
            authParent.setToken(STP.getTokenValue());
            STP.getTokenSession().set(CacheConstants.LOGIN_USER_KEY, authParent);
        }
        SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, authParent);
        return authParent;
    }

    private static void configParentStpLogic() {
        SaTokenConfig config = new SaTokenConfig();
        config.setTokenName("p-token");
        config.setTokenStyle("simple-uuid");
        config.setIsReadBody(false);
        config.setIsReadCookie(false);
        STP.setConfig(config);
    }
}
